<script>
export default {
  name: 'ImprovementSuggestion',
  props: {
    queryForm: {
      type: Object,
      required: true,
      default: {}
    },
    projectList: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    queryForm: {
      async handler(val) {
        if(val.sceneProjectId != null){
          const param = {
            statisticsMode: val.statisticsMode,
            sceneProjectId: val.sceneProjectId,
            startDate: val.startTime,
            endDate: val.endTime
          }
          if (val.roomInfo != null && val.roomInfo.length > 0) {
            param.sceneEdificeId = val.roomInfo[0]
            if (val.roomInfo.length > 1) {
              param.sceneFloorId = val.roomInfo[1]
            }
            if (val.roomInfo.length > 2) {
              param.sceneRoomId = val.roomInfo[2]
            }
          }
          if (param.sceneProjectId == null && this.projectList.length > 0) {
            param.sceneProjectId = this.projectList[0].value
          }
          if (param.sceneProjectId != null) {
            let res = await this.$api.userEnergyOptimizationSuggestions.energyConsumptionDescription(param)
            this.startTime = res.data.startDate
            this.endTime = res.data.endDate
            this.energyAnalysisData = res.data.dataList

            res = await this.$api.userEnergyOptimizationSuggestions.photovoltaicPowerAnalysis(param)
            this.dateList = res.data.dateList
            this.values1 = res.data.values1
            this.values2 = res.data.values2
            this.values3 = res.data.values3
            this.values4 = res.data.values4
            await this.getConfigContent(this.getValues4Avg)

            res = await this.$api.userEnergyOptimizationSuggestions.energyStorageAnalysis(param)
            this.userEnergyValues1 = res.data.values1
            this.userEnergyValues2 = res.data.values2

            //充电桩
            res = await this.$api.userEnergyOptimizationSuggestions.chargingStationAnalysis(param)
            this.chargingStationValues1 = res.data.values1
            this.chargingStationValues2 = res.data.values2
        }
        }
      },
      deep: true,
      immediate: true
    }
  },
  computed: {
    getProjectName() {
      let sceneProjectId = this.queryForm.sceneProjectId
      if (sceneProjectId == null && this.projectList.length > 0) {
        sceneProjectId = this.projectList[0].value
      }
      return this.projectList.find(project => project.value === sceneProjectId)?.label
    },
    getMaxEnergyAnalysisData() {
      if (this.energyAnalysisData.length > 0) {
        return this.energyAnalysisData.slice().sort((a, b) => b.value - a.value)[0]
      }
      return {}
    },
    getMinEnergyAnalysisData() {
      if (this.energyAnalysisData.length > 0) {
        return this.energyAnalysisData.slice().sort((a, b) => a.value - b.value)[0]
      }
      return {}
    },
    // 光伏发电自消纳率
    getValues4Avg() {
      return this.values4.length === 0 ? 0 : this.values4.reduce((a, b) => a + b, 0) / this.values4.length
    }
  },
  data() {
    return {
      energyAnalysisData: [],
      startTime: null,
      endTime: null,
      dateList: [],
      values1: [],
      values2: [],
      values3: [],
      values4: [],
      userEnergyValues1: [],
      userEnergyValues2: [],
      chargingStationValues1: [],
      chargingStationValues2: [],
      content: null
    }
  },
  methods: {
    async getConfigContent(avg) {
      let name = ''
      if (avg >= 95) {
        name = '光伏发电消纳率95%及以上'
      } else if (avg >= 75) {
        name = '光伏发电消纳率75%~95%'
      } else {
        name = '光伏发电消纳率75%以下'
      }
      const response = await this.$api.optimizationSuggestion.getContent({
        name: name
      })
      this.content = response.data
    }
  }
}
</script>

<template>
  <div>
    <h2>08、优化建议</h2>
    <!--    <p>-->
    <!--      项目1 在2022年01月02日-2022年01月08日-->
    <!--      中，能耗总量为80kgce，其中电能能耗量最高，为150kgce；能源5能耗量最低，为10kgCO2。柔性源柔性度总量为1000kw；光伏发电量总量为100kwh，-->
    <!--      其中光伏发电自消纳率为80%；储能充电总量为500kwh，储能放电总量为300kwh；充电桩充电总量为300kwh，充电桩放电总量为200kwh。AI模型总收益为70元，总费用为70元，减排量为10kgCO2。-->
    <!--    </p>-->
    <div>
      <p>优化建议：</p>
      <p>
        {{ getProjectName }} 在{{ startTime }}-{{ endTime }}
        中，能耗总量为{{ energyAnalysisData.map(item => item.value).reduce((a, b) => a + b, 0) }} kgce，
        其中{{ getMaxEnergyAnalysisData.name }}能耗量最高，为{{ getMaxEnergyAnalysisData.value }}
        kgce；{{ getMinEnergyAnalysisData.name }}能耗量最低，为{{ getMinEnergyAnalysisData.value }}kgCO2。
        柔性源柔性度总量为1000kw；
        光伏发电量总量为{{ values1.reduce((a, b) => a + b, 0) }}kwh，其中光伏发电自消纳率为{{ getValues4Avg }}%；
        储能充电总量为{{ userEnergyValues1.reduce((a, b) => a + b, 0) }}kwh，
        储能放电总量为{{ userEnergyValues2.reduce((a, b) => a + b, 0) }}kwh；
        充电桩充电总量为{{ chargingStationValues1.reduce((a, b) => a + b, 0) }} kwh，
        充电桩放电总量为{{ chargingStationValues2.reduce((a, b) => a + b, 0) }} kwh。
        AI模型总收益为70元，总费用为70元，减排量为10kgCO2。
      </p>
      <p v-show="content != null" class="content" v-html="content"></p>
    </div>
  </div>
</template>

<style scoped lang="scss">
p {
  text-indent: 2em;
}

.content {
  white-space: pre-wrap;
  line-height: 20px;
  text-indent: 0;
  padding-left: 4%;
}
</style>
